$options = {
  :dont_get => false,
  :dry_run => false,
  :log_to_file => true,
  :verbose => false,
  :list_products => false
}

# defaults
$modifiers = {
  'installer' => 'latest',
  'autorun2' => 'latest',
  'engine1.9' => 'new',
  'no_single' => false
}

# Build machine enviroment

# this is global Environment variables container
$E = { :BMD => File.dirname( __FILE__ ) }
  # Blueprints dir.
  $E[:BPs] = File.join( $E[:BMD], 'blueprints' )
  # Library dir. All '.rb' files located here will be required.
  $E[:Lib] = File.join( $E[:BMD], 'lib' )
  # Run dir. All work will be performed here.
  $E[:Run] = File.join( $E[:BMD], 'run' )
  #Custom data for misc. purposes
  $E[:Cst] = File.join( $E[:BMD], 'custom' )

  $E[:Hst] = File.join( $E[:BMD], 'history' )

# require modules from ./lib/
$stderr.puts "loading modules from #{$E[:Lib]}" if $options[:verbose]
Dir.glob( "#{$E[:Lib]}/*.rb" ).each{|r|
  require r
  $stderr.puts "Found lib: #{r}" if $options[:verbose]
}

if $0 == __FILE__

# require statements
require 'optparse'
require 'fileutils'
include FileUtils

#~ $stdout = File.open("main_#{Time.now.strftime("%Y_%m_%d_%H_%M_%S")}.log", 'w')

#
# Cmdline Options
#

OptionParser.new do |opts|
  opts.banner = "Insert some disclaimer here"

  opts.separator ""
  opts.separator "Specific options:"

  opts.on("-p PRODUCT", "--product PRODUCT", "select PRODUCT to act on. ") do |p|
    raise "--product option is nesesary" if p.nil?
    $options[:product] = p
  end

  opts.on("-g", "--[no-]get", "Do not get files from cvs. Default: false.") do |p|
    $options[:dont_get] = ! p
  end

  opts.on("-d", "--[no-]dry-run", "Do not perform commands, print them only. Default: false. (devel)") do |p|
    $options[:dry_run] = p
  end

  opts.on("-l", "--list", "List available products or versions for selected product. (see -p)") do |p|
    $options[:list] = p
  end

  opts.on("-b", "--build", "build selected product. (see -p)") do |p|
    $options[:build] = p
  end

  opts.on("-m", "--modifiers MODIFIERS", "build selected product. (see -p)") do |m|
    m.split(',').each{|mod|
      k,v = mod.split('=')
      $modifiers[k] = (v || true)
    }

  end

  opts.separator ""
  opts.separator "Common options:"

  opts.on("-v", "--[no-]verbose", "Print some additional info. default: false") do |p|
    $options[:verbose] = p
  end

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end.parse!(ARGV)

#
# Main stream
#
  p $options if $options[:verbose]

  if $options[:product]
    # global logger initialization
    $l = Simlogger.new( File.join( $E[:Hst], "main_#{Time.now.strftime("%Y.%m.%d.%H.%M.%S")}.log" ) )
    Product.init($l)

    $l.update( {:type => :log, :content => "options:#{$options.inspect}"} )
    $l.update( {:type => :log, :content => "modifiers:#{$modifiers.inspect}"} )

    p = Product.new($options[:product])

    p.build if $options[:build]
    puts p.list if $options[:list]
  elsif $options[:list]
    $stdout.puts "#{Product.list.join "\n"}"
  else
    puts "nothing to do. see help (--help)"
  end

end
